:: 与 sqlite3 源码一起编译
setlocal
chcp 65001
@echo off
:: 准备工作
:: 准备 sqlite 
set "EXTRACT_TO_FOLDER=cache"
if not exist "%EXTRACT_TO_FOLDER%\" (  
    mkdir "%EXTRACT_TO_FOLDER%"  
)
:: 注意设置文件夹路径(需要和压缩包文件相同)
set "ZIP_FILE_DIR=sqlite-amalgamation-3460000"
if not exist "%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%/sqlite3.h" (
    if not exist "%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip" (
        echo 正在下载 sqlite3 源码
        echo 如果下载太慢, 手工下载并移动到文件夹[%EXTRACT_TO_FOLDER%]下, 并重命名为 [%ZIP_FILE_DIR%.zip]
        echo 下载链接: https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip
        curl -o "%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip" "https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip"
        echo 下载成功
    )
    echo 正在解压文件 [%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip]
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%.zip', '%EXTRACT_TO_FOLDER%'); }"
)

:: 准备生成动态链接库文件夹
set "TARGET_DIR=objs"
set "TARGET_DLL=libsqlite3cj.dll"
if not exist "%TARGET_DIR%\" mkdir "%TARGET_DIR%"
if exist "%TARGET_DIR%\%TARGET_DLL%" del "%TARGET_DIR%\%TARGET_DLL%"

@echo on
:: 生成 dll 动态文件
gcc %EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR%/sqlite3.c -I./%EXTRACT_TO_FOLDER%/%ZIP_FILE_DIR% sqlite3_ext_cj.c -Os -lpthread -lm -shared -fPIC -o %TARGET_DIR%/%TARGET_DLL%
:: gcc cache/sqlite-amalgamation-3460000/sqlite3.c -I./cache/sqlite-amalgamation-3460000 sqlite3_ext_cj.c -Os -lpthread -lm -shared -fPIC -o objs/libsqlite3cj.dll

@echo off
echo.
echo 已生成 %TARGET_DIR%\%TARGET_DLL%
echo.
echo 注意: 不要和SQLite原生的 sqlite3.dll 文件一起使用
echo.
pause
